package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.dao.SkillCategoryMapper;
import com.mmall.pojo.SkillCategory;
import com.mmall.service.ISkillCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @CLASS SkillCategoryServiceimpl
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-23 16:26
 * @VERSION 1.0
 **/
@Service("iSkillCategoryService")
public class SkillCategoryServiceimpl implements ISkillCategoryService {

    @Autowired
    private SkillCategoryMapper skillCategoryMapper;

    public ServerResponse<PageInfo> list(Integer userId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SkillCategory> skillCategoryList = skillCategoryMapper.selectUserIdSkill(userId);
        PageInfo pageInfo = new PageInfo(skillCategoryList);
        pageInfo.setList(skillCategoryList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse<String> updateServe(Integer userId,SkillCategory skillCategory){
        skillCategory.setUserId(userId);
        // 修改
        if (skillCategory.getId()!=null){
            int count = skillCategoryMapper.updateByPrimaryKeySelective(skillCategory);
            if (count>0){
                return ServerResponse.createBySuccessMessage("修改文章类目成功");
            }
            // 新增
        }else {
            int count = skillCategoryMapper.insert(skillCategory);
            if (count>0){
                return ServerResponse.createBySuccessMessage("新增文章类目成功");
            }
        }
        return ServerResponse.createByErrorMessage("新增修改文章类目失败");
    }

    public ServerResponse<String> delete(Integer userId,Integer skillCategoryId){
        if (skillCategoryId==null){
            ServerResponse.createByErrorMessage("文章类目id不能为空");
        }
        int count = skillCategoryMapper.deleteSkillCategoryUserId(userId,skillCategoryId);
        if (count>0) return ServerResponse.createBySuccessMessage("删除文章类目成功");
        return ServerResponse.createByErrorMessage("删除文章类目失败");
    }

    public ServerResponse<SkillCategory> detail(Integer userId,Integer skillCategoryId){
        if (skillCategoryId==null){
            ServerResponse.createByErrorMessage("文章类目id不能为空");
        }
        SkillCategory article = skillCategoryMapper.selectByPrimaryKey(skillCategoryId);
        return ServerResponse.createBySuccess(article);
    }
}
